package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppRoomCollectDao;
import com.vf.admin.client.dao.AppRoomDao;
import com.vf.admin.client.dao.AppRoomImgDao;
import com.vf.admin.client.dao.AppRoomTypeDao;
import com.vf.admin.client.service.*;
import com.vf.admin.client.tx.utils.TxIm;
import com.vf.admin.common.Constants;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class AppRoomServiceImpl implements AppRoomService {

    @Autowired
    AppRoomDao appRoomDao;
    @Autowired
    AppRoomCollectDao appRoomCollectDao;
    @Autowired
    AppRoomTypeDao appRoomTypeDao;
    @Autowired
    AppRoomImgDao appRoomImgDao;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppMingXiZuanShiService appMingXiZuanShiService;
    @Autowired
    AppMingxiMeilizhiService appMingxiMeilizhiService;
    @Autowired
    AppFansClubService appFansClubService;
    @Autowired
    AppTaskRecordService appTaskRecordService;
    @Autowired
    AppMingxiRoomService appMingxiRoomService;
    @Autowired
    RedisUtil redisUtil;


    @Override
    public List<AppRoomType> getRoomAllCat(Integer xingzhi) {
        return appRoomTypeDao.list(xingzhi);
    }

    @Override
    public List<AppRoom> getSearch(String sousuo, SplitPageBean page) {
        return appRoomDao.getSearch(sousuo, page);
    }

    /**
     * 房间推荐
     */
    @Override
    public List<AppRoom> roomTuiJianList(Integer type, SplitPageBean page) {
        return appRoomDao.roomTuiJianList(type, page);
    }

    @Override
    public AppRoom getRoomCode(String roomcode) {
        return appRoomDao.getRoomCode(roomcode);
    }

    @Override
    public AppRoom getTxCode(Integer txcode) {
        return appRoomDao.getTxCode(txcode);
    }

    @Override
    public List<AppRoom> getTypeRoomList(Integer xingzhi, SplitPageBean splitPageBean) {
        return appRoomDao.getTypeRoomList(xingzhi, splitPageBean);
    }

    @Override
    public List<AppRoom> getRoomAll() {
        return appRoomDao.getRoomAll();
    }

    @Override
    public List<AppRoom> getRoomFiveMinute() {
        return appRoomDao.getRoomFiveMinute();
    }

    @Override
    public List<AppRoom> getPiliang(String arr) {
        return appRoomDao.getPiliang(arr);
    }

    @Override
    public AppRoom getById(Integer id) {
        return appRoomDao.get(id);
    }

    @Override
    public AppRoom getUserId(Integer userid) {
        return appRoomDao.getUserId(userid);
    }

    @Override
    public void uproomimg(Integer roomid, String img) {
        appRoomDao.uproomimg(roomid, img);
    }

    @Override
    public void uproomtype(Integer roomid, Integer roomtypeid, String roomtypename, Integer xingzhi) {
        appRoomDao.uproomtype(roomid, roomtypeid, roomtypename, xingzhi);
    }

    @Override
    public void uproomname(Integer roomid, String roomname) {
        appRoomDao.uproomname(roomid, roomname);
    }

    @Override
    public void updateliwutexiao(Integer roomid, Integer leixing) {
        appRoomDao.updateliwutexiao(roomid, leixing);
    }

    @Override
    public void updatejinfangtishi(Integer roomid, Integer leixing) {
        appRoomDao.updatejinfangtishi(roomid, leixing);
    }

    @Override
    public void updateroomkaiguan(Integer roomid, Integer leixing, String pass) {
        appRoomDao.updateroomkaiguan(roomid, leixing, pass);
    }

    @Override
    public void updateroomgonggao(Integer roomid, String gonggao) {
        appRoomDao.updateroomgonggao(roomid, gonggao);
    }

    @Override
    public void updateRoomZhiboState(Integer roomid, Integer zaixian) {
        appRoomDao.updateRoomZhiboState(roomid, zaixian);
    }

    @Override
    public void uproombeijing(Integer roomid, String roombeijing, Integer roombeiimgid) {
        appRoomDao.uproombeijing(roomid, roombeijing, roombeiimgid);
    }

    @Override
    public void save(AppRoom room) {
        appRoomDao.save(room);
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean creatRoom(AppUser user, Integer leixing, String roomtitle, String image, String gonggao, Integer beijing, Integer pmleixing,String lon,String lat) {
        try {
            AppRoom appRoom = appRoomDao.getUserId(user.getId());
            AppRoom room = new AppRoom();
            //判断用户是否已创建过房间
            if (!StringUtils.isEmpty(appRoom)) {
                room.setId(appRoom.getId());
                room.setRoomcode(appRoom.getRoomcode());
            } else {
                //并未创建房间
                //生成房间账号
                //获取usercode
                Random random=new Random();
                long roomCodeSize = redisUtil.lGetListSize("chi:roomcode");
                int roomCodeIndex = random.nextInt((int) roomCodeSize);
                Integer roomcode = Integer.valueOf(redisUtil.lGetIndex("chi:roomcode", roomCodeIndex).toString());
                redisUtil.lRemove("chi:roomcode", 1, roomcode+"");
                room.setRoomcode(roomcode);
                room.setUserid(user.getId());
            }
            room.setPic(user.getPic());
            room.setPhone(user.getLoginname());
            room.setTengxuncode(user.getTengxuncode());
            room.setNick(user.getNick());
            room.setXingzhi(leixing);
            room.setRoompass("");
            room.setRoomname(roomtitle);
            room.setRoomimg(image);
            room.setRoomnotice(gonggao);
            room.setStatus(1);
            room.setGifteffects(1);
            room.setIntoroomstate(1);
            room.setPassstate(2);
            room.setOnlinenum(0);
            room.setRoomstatus(2);
            room.setKbzhuangtai(1);
            AppRoomImg roomImg = appRoomImgDao.get(beijing);
            room.setRoombeiimg(roomImg.getUrl());
            room.setRoombeiimgid(beijing);
            room.setLon(lon);
            room.setLat(lat);
            if (leixing == 1) {
                //若房间类型为语音直播，需要添加额外参数
                room.setRoomboos(1);
                room.setRoommood(8);
                room.setRoompaimai(pmleixing);
                room.setOnlinenum(0);
            }
            room.setCreatetime(new Date());
            room.setChamopen(new Date());
            appRoomDao.save(room);
            //修改收藏的房间信息
            AppRoomCollect appRoomCollect = appRoomCollectDao.getRoomcode(room.getRoomcode());
            if (!StringUtils.isEmpty(appRoomCollect)) {
                //更新收藏
                appRoomCollectDao.uodateData(room.getRoomcode(), room.getXingzhi(), room.getRoomtypeid(), room.getRoomtypename(), room.getRoomname(), room.getRoomimg());
            }
            return true;

        } catch (Exception e) {
            //其他原因失败，事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean txCreatRoom(AppUser user, AppRoom room) {
        try {
            //调用创建房间
            int errcode = TxIm.createGroup(user.getTengxuncode() + "", user.getTengxuncode() + "", room.getRoomname());
            //判断是否成功导入
            if (errcode == Constants.TX_ERRCODED_SUCESS || errcode == Constants.TX_ERRCODED_qz) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            //其他原因失败，事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    public void updateroomshi(Integer roomid, String shi) {
        appRoomDao.updateroomshi(roomid, shi);
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean joinTheFanClub(AppUser user, BigDecimal fensituan, AppRoom room) {
        try {
            AppSetup setup = appSetupService.get();
            //支付钻石
            appUserService.upJianZuanShi(user.getId(), fensituan);
            //添加钻石明细
            appMingXiZuanShiService.addMingxi(user.getId(), fensituan, 2, "加入粉丝团");
            //给主播返利
            int libi = setup.getFensituanfanli();
            appUserService.updatemeili(room.getUserid(), libi);
            //添加魅力值明细
            appMingxiMeilizhiService.addMingxi(room.getUserid(), libi, 1, "加入粉丝团");
            //保存粉丝团记录
            appFansClubService.fensiSave(room.getRoomcode(), room.getId(), user);
            //添加房间流水
            appMingxiRoomService.addMIngxi(user, fensituan, 1, 2, "加入粉丝团", room);
            return true;
        } catch (Exception e) {
            //其他原因失败，事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    public List<AppRoom> getByNearbyUser(String lon, String lat, SplitPageBean splitPageBean){
        return appRoomDao.getByNearbyUser(lon, lat, splitPageBean);
    }
}
